package spotGUI.floatingChatGUI;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import managers.conversationManager.services.ReceiveFile;
import spotGUI.utils.EmptyContentAreaButton;
import java.util.ResourceBundle;

public class ReceiveFilePanel extends JPanel implements ReceiveFileFeedback {

	private Box fileTransferBox;
	private JButton btnAcceptFT;
	private JButton btnRefuseFT;
	private JProgressBar progressBar;
	private Component horizontalStrut;
	private Component horizontalStrut_1;
	private JLabel lblInfo;
	private JButton btnHide;
	
	private ReceiveFile currentFileTransfer;


	public ReceiveFilePanel() {
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{16, 21, 0};
		gridBagLayout.rowHeights = new int[]{22, 24, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		btnHide = new EmptyContentAreaButton(ReceiveFilePanel.class.getResource("/images/Icons/16x16px/delete.png"), "");
		btnHide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		GridBagConstraints gbc_btnHide = new GridBagConstraints();
		gbc_btnHide.anchor = GridBagConstraints.WEST;
		gbc_btnHide.fill = GridBagConstraints.VERTICAL;
		gbc_btnHide.insets = new Insets(0, 0, 5, 5);
		gbc_btnHide.gridx = 0;
		gbc_btnHide.gridy = 0;
		add(btnHide, gbc_btnHide);

		lblInfo = new JLabel();
		GridBagConstraints gbc_lblInfo = new GridBagConstraints();
		gbc_lblInfo.fill = GridBagConstraints.BOTH;
		gbc_lblInfo.insets = new Insets(0, 0, 5, 0);
		gbc_lblInfo.gridx = 1;
		gbc_lblInfo.gridy = 0;
		add(lblInfo, gbc_lblInfo);

		fileTransferBox = Box.createHorizontalBox();
		GridBagConstraints gbc_fileTransferBox_1 = new GridBagConstraints();
		gbc_fileTransferBox_1.fill = GridBagConstraints.BOTH;
		gbc_fileTransferBox_1.gridx = 1;
		gbc_fileTransferBox_1.gridy = 1;
		add(fileTransferBox, gbc_fileTransferBox_1);

		btnAcceptFT = new JButton(ResourceBundle.getBundle("spotGUI.floatingChatGUI.messages").getString("ReceiveFilePanel.btnAcceptFT.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnAcceptFT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeChoice(ReceiveFile.FT_ACCEPT);
			}
		});
		fileTransferBox.add(btnAcceptFT);

		horizontalStrut = Box.createHorizontalStrut(10);
		fileTransferBox.add(horizontalStrut);

		btnRefuseFT = new JButton(ResourceBundle.getBundle("spotGUI.floatingChatGUI.messages").getString("ReceiveFilePanel.btnRefuseFT.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnRefuseFT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeChoice(ReceiveFile.FT_REFUSE);
			}

		});
		fileTransferBox.add(btnRefuseFT);

		horizontalStrut_1 = Box.createHorizontalStrut(20);
		fileTransferBox.add(horizontalStrut_1);

		progressBar = new JProgressBar();
		progressBar.setVisible(false);
		progressBar.setIndeterminate(true);
		progressBar.setOpaque(false);
		progressBar.setString("");
		progressBar.setStringPainted(true);
		fileTransferBox.add(progressBar);
		
		setVisible(false);
	}
	
	public void updateLogBar (String text, ImageIcon icon) {
		lblInfo.setText(text);
		lblInfo.setIcon(icon);
	}
	
	
	private void makeChoice(int choice) {
		currentFileTransfer.setChoice(choice);
		currentFileTransfer.wakeUp();
	}
	
	public void lock (boolean lock) {
		btnHide.setEnabled(!lock);
	}

	public void setCurrentFileTransfer(ReceiveFile ft) {
		this.currentFileTransfer = ft;
		
	}

	public void setReceiving (boolean receiving) {
		progressBar.setVisible(receiving);
	}

	public void enableChoiceButtons(boolean enabled) {
		btnAcceptFT.setEnabled(enabled);
		btnRefuseFT.setEnabled(enabled);
	}
}
